<template>
  <div class="back-to-top" @click="backToTop" v-show="showReturnToTop" @mouseenter="show" @mouseleave="hide">
    <!--<i :class="[bttOption.iClass]" :style="{color:bttOption.iColor,'font-size':bttOption.iFontsize}"></i>-->
    <i class='tri'></i>
    <!--<span class="tips" :class="[bttOption.iPos]" :style="{color:bttOption.textColor}" v-show="showTooltips">{{bttOption.text}}</span>-->
  </div>
</template>

<script>
import { scrollIt } from '../index' // 引入动画过渡的实现
export default {
  name: 'back-to-top',
  props: {
    text: { // 文本提示
      type: String,
      default: '返回顶部'
    },
    textColor: { // 文本颜色
      type: String,
      default: '#f00'
    },
    iPos: { // 文本位置
      type: String,
      default: 'right'
    },
    iClass: { // 图标形状
      type: String,
      default: 'fzicon fz-ad-fanhuidingbu1'
    },
    iColor: { // 图标颜色
      type: String,
      default: '#f00'
    },
    iFontsize: { // 图标大小
      type: String,
      default: '32px'
    },
    pageY: { // 默认在哪个视图显示返回按钮
      type: Number,
      default: 200
    },
    transitionName: { // 过渡动画名称
      type: String,
      default: 'linear'
    }
  },
  data: function () {
    return {
      showTooltips: false, // 文字提示
      showReturnToTop: false
    }
  },
  computed: {
    bttOption () {
      return {
        text: this.text,
        textColor: this.textColor,
        iPos: this.iPos,
        iClass: this.iClass,
        iColor: this.iColor,
        iFontsize: this.iFontsize
      }
    }
  },
  methods: {
    show () { // 显示隐藏提示文字
      this.showTooltips = true
    },
    hide () {
      this.showTooltips = false
    },
    currentPageYOffset () {
      // 判断滚动区域大于多少的时候显示返回顶部的按钮
      window.pageYOffset > this.pageY ? this.showReturnToTop = true : this.showReturnToTop = false
    },
    backToTop () {
      scrollIt(0, 800, this.transitionName, this.currentPageYOffset)
    }
  },
  created () {
    window.addEventListener('scroll', this.currentPageYOffset)
  },
  // beforeDestroy 表示销毁组件的dom或者相关的js(表示显示的是销毁前的状态) destroyed 销毁完成后
  beforeDestroy () {
    window.removeEventListener('scroll', this.currentPageYOffset)
  }
}
</script>

<style scoped lang="scss">
  .back-to-top {
    position: fixed;
    bottom: 10%;
    right: 25px;
    z-index: 9999;
    cursor: pointer;
    width: auto;
    i {
      width: 0;
      height: 0;
      border: 12px solid transparent;
      border-bottom-color: #dfe6ec;
      text-align: center;
      font-size: 32px;
      display: inline-block;
      position: relative;
      padding: 5px;
      border-radius: 5px;
      transition: all 0.3s linear;
      background: #cccccc;
      &:hover {
        background: rgba(40,161,251,0.5);
        color: #fff !important;
      }
    }
    .tips {
      display: inline-block;
      position: absolute;
      word-break: normal;
      white-space: nowrap;
      width: auto;
      font-size: 12px;
      color: #fff;
      z-index: -1;
    }
    .left {
      right: 0;
      top: 50%;
      margin-right: 50px;
      transform: translateY(-50%);
    }
    .right {
      left: 0;
      top: 50%;
      margin-left: 50px;
      transform: translateY(-50%);
    }
    .bottom {
      bottom: 0;
      margin-top: 50px;
    }
    .top {
      top: 0;
      margin-bottom: 50px;
    }
  }
</style>
